<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="商圈名">
              <a-input placeholder="请输入商圈名" v-model="queryParam.name" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="3" :lg="3" :md="8" :sm="8">
            <a-form-item label="省">
              <a-select default-value="1" placeholder="全部省" v-model="queryParam.provinceId" @change="provinceIdChange">
                <a-select-option :value="item.val" v-for="(item, index) in provinceUl" :idt="item.id" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="3" :lg="3" :md="8" :sm="8">
            <a-form-item label="市">
              <a-select default-value="1" placeholder="全部市" v-model="queryParam.cityId" @change="cityIdChange">
                <a-select-option :value="item.val" v-for="(item, index) in cityUl" :idt="item.id" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="3" :lg="3" :md="8" :sm="8">
            <a-form-item label="区">
              <a-select default-value="1" placeholder="全部区" v-model="queryParam.districtId" >
                <a-select-option :value="item.val" v-for="(item, index) in districtUl" :idt="item.id" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons" >
              <a-button type="primary" @click="searchQuery" icon="search" >查询</a-button >
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px" >重置</a-button >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="crudCreate" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table 
        ref="table" 
        size="middle" 
        bordered
        rowKey="id" 
        :scroll="{ x: 1300 }"
        :columns="columns" 
        :dataSource="tableData" 
        :pagination="ipagination" 
        :loading="loading" 
        @change="handleTableChange"
      >
        <!--操作-->
        <span slot="action" slot-scope="text, record" >
          <a-button type="primary" @click="crudUpdate(record)" icon="edit"></a-button>
          <a-popconfirm title="确定删除吗?" @confirm="() => crudDelete(record.id)" okText="删除" cancelText="取消">
            <a-button type="danger" icon="delete" />
          </a-popconfirm>
        </span>
        <!--图片预览块-->
        <span slot="imgPath" slot-scope="text, record">
          <img :src="record.img" width="100px" height="100px" />
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <business-area-modal ref="updateForm" @closeModal="closeModal"></business-area-modal>
  </a-card>
</template>

<script>
import businessAreaModal from './modules/businessAreaModal'
import ApiTree from "@/api/common/businessArea";
import City from "@/api/common/city";
import { Slime, crud } from "@/mixins/slime";

export default {
  name: "city",
  mixins: [Slime],
  components: {
    businessAreaModal
  },
  data() {
    return {
      provinceUl:[],
      cityUl:[],
      districtUl:[],

      tableData: [],
      columns: [
        {
          title: "商圈名",
          align: "center",
          dataIndex: "name",
        },
        {
          title: "省份",
          align: "center",
          dataIndex: "provinceName",
        },
        {
          title: "城市",
          align: "center",
          dataIndex: "cityName",
        },
        {
          title: "区/县",
          align: "center",
          dataIndex: "districtName",
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
    };
  },
  created() {
    crud(ApiTree);
    this.mapList()
    this.loadData();
  },
  methods: {
    // 加载表格数据
    loadData(arg) {
      this.loadDataPack(arg).then((res) => {
        let tableData = [];
        if (res.result) {
          tableData = res.result.records;
        }
        this.tableData = tableData;
      });
    },
    //地区
    mapList(){
      let that = this
      City.regionList(0).then((res) => {
        let provinceUl = []
        let data = res.result
        data.map((obj)=>{
          let li = {
            name: obj.name,
            id: obj.id,
            val: obj.code,
          }
          provinceUl.push(li)
        })
        that.provinceUl = provinceUl
        //存在省级
        if(that.queryParam.shengId){
          City.regionList(that.queryParam.shengId).then((res) => {
            let cityUl = []
            let data = res.result
            data.map((obj)=>{
              let li = {
                name: obj.name,
                id: obj.id,
                val: obj.code,
              }
              cityUl.push(li)
            })
            that.cityUl = cityUl
            //存在市级
            if(that.queryParam.shiId){
              City.regionList(that.queryParam.shiId).then((res) => {
                let districtUl = []
                let data = res.result
                data.map((obj)=>{
                  let li = {
                    name: obj.name,
                    id: obj.id,
                    val: obj.code,
                  }
                  districtUl.push(li)
                })
                that.districtUl = districtUl
              })
            }
          })
        }
      })
    },
    provinceIdChange(value,option){
      let provinceId = option.data.attrs.idt
      this.queryParam.shengId = option.data.attrs.idt
      City.regionList(provinceId).then((res) => {
        let cityUl = []
        let data = res.result
        data.map((obj)=>{
          let li = {
            name: obj.name,
            id: obj.id,
            val: obj.code,
          }
          cityUl.push(li)
        })
        this.cityUl = cityUl
      })
    },
    cityIdChange(value,option){
      let cityId = option.data.attrs.idt
      this.queryParam.shiId = option.data.attrs.idt
      City.regionList(cityId).then((res) => {
        let districtUl = []
        let data = res.result
        data.map((obj)=>{
          let li = {
            name: obj.name,
            id: obj.id,
            val: obj.code,
          }
          districtUl.push(li)
        })
        this.districtUl = districtUl
      })
    },
    //详情
    closeModal(){
      this.loadData();
    }
  },
};
</script>
<style scoped>
</style>